// pages/myorder/myorder.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadmore: 'block',
    nomore: 'none',
    nodata: 'none',
    page: 1,
    list: [],
    state: -1,
    refresh: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ state: parseInt(options.state) });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.refresh) {
      this.setData({ list: [], page: 1, loadmore: 'block', nomore: 'none' });
      this.Find_POrder();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page > 1) {  //必须加上，否则上拉和下拉会同时触发
      this.Find_POrder();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  openOrderInfo: function (e) {
    this.setData({ refresh: false });
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/orderinfo/orderinfo?orderid=' + id
    })
  },
  Find_POrder: function () {
    this.setData({ refresh: true });
    var that = this;
    var data = {};
    data.act = "Find_POrder";
    data.page = that.data.page;
    data.state = that.data.state;
    wx.showLoading({
      title: '数据加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      var pagecount = Math.floor(Math.abs(json.count - 1) / json.pagesize) + 1;
      if (data.page > pagecount) { return; }
      that.setData({ list: that.data.list.concat(json.data), page: data.page + 1 });
      console.log(json);
      if (that.data.list.length == 0) {
        that.setData({ loadmore: 'none', nodata: 'block', nomore: 'none' });
      }
      else if (pagecount == 1) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'none' });
      }
      else if (data.page < pagecount) {
        that.setData({ loadmore: 'block', nodata: 'none', nomore: 'none' });
      } else if (data.page == pagecount) {
        that.setData({ loadmore: 'none', nodata: 'none', nomore: 'block' });
      }
      wx.stopPullDownRefresh();
    });
  },
  Delete_POrder: function (e) {
    var id = e.currentTarget.dataset.id;
    var tip = e.currentTarget.dataset.tip;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmColor: '#fb4747',
      content: '您确定要' + tip + '该订单吗？',
      success: function (res) {
        if (res.confirm) {
          var data = {};
          data.ids = id;
          data.act = "Delete_POrder";
          wx.showLoading({
            title: tip + '中...',
          })
          app.Ajax(data, function (json) {
            wx.hideLoading();
            if (json.state) {
              var list = that.data.list;
              for (var i = list.length - 1; i >= 0; i--) {
                if (list[i].ID == id) {
                  list = list.RemoveAt(i);
                }
              }
              that.setData({ list: list });
              app.showSuccess(tip + '成功');
            } else {
              app.showError(tip + '失败');
            }
          });
        }
      }
    })
  },
  Sure_POrder: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmColor: '#fb4747',
      content: '您确定要收货吗？',
      success: function (res) {
        if (res.confirm) {
          var data = {};
          data.id = id;
          data.act = "Sure_POrder";
          wx.showLoading({
            title: '确认中...',
          })
          app.Ajax(data, function (json) {
            wx.hideLoading();
            if (json.state) {
              var list = that.data.list;
              for (var i = list.length - 1; i >= 0; i--) {
                if (list[i].ID == id) {
                  list = list.RemoveAt(i);
                }
              }
              that.setData({ list: list });
              app.showSuccess(json.msg);
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/reply/reply?orderid=' + id
                })
              }, 1000);
            } else {
              app.showError(json.msg);
            }
          });
        }
      }
    })
  },
  Remind_POrder: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var data = {};
    data.act = "Remind_POrder";
    data.id = id;
    wx.showLoading({
      title: '请稍候...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      if (json.state) {
        app.showSuccess(json.msg);
      } else {
        app.showError(json.msg);
      }
    });
  },
  openReply: function (e) {
    this.setData({ refresh: false });
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/reply/reply?orderid=' + id
    })
  },
  openPost: function (e) {
    var id = e.currentTarget.dataset.id;
    var logistics = e.currentTarget.dataset.logistics;
    var expressnum = e.currentTarget.dataset.expressnum;
    var img = e.currentTarget.dataset.img;
    this.setData({ refresh: false });
    wx.navigateTo({
      url: '/pages/post/post?from=myorder&orderid=' + id + '&logistics=' + logistics + "&expressnum=" + expressnum + "&img=" + img
    })
  },
  openPay: function (e) {
    var id = e.currentTarget.dataset.id;
    var ordernum = e.currentTarget.dataset.ordernum;
    var realname = e.currentTarget.dataset.realname;
    var mobile = e.currentTarget.dataset.mobile;
    var address = e.currentTarget.dataset.address;
    var money = e.currentTarget.dataset.money;
    var data={};
    data.openid = app.User.OpenID;
    data.allmoney=money;
    data.id=id;
    data.act = "GetWxParam2";
    wx.showLoading({
      title: '正在调起支付...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      wx.requestPayment({
        'timeStamp': json.data.timestamp,
        'nonceStr': json.data.noncestr,
        'package': json.data.package,
        'signType': 'MD5',
        'paySign': json.data.sign,
        'success': function (res) {
          wx.navigateTo({
            url: '/pages/paysuccess/paysuccess?ordernum=' + ordernum + "&realname=" + realname + "&mobile=" + mobile + "&address=" + address + "&money=" + money
          })
        },
        'fail': function (res) {
        }
      })
    });

    
  },
  changetab: function (e) {
    var state = e.currentTarget.dataset.state;
    this.setData({ list: [], page: 1, loadmore: 'block', nomore: 'none', nodata: 'none', state: state });
    this.Find_POrder();
  }
})